import sqlite3, hashlib, json, datetime
from django.shortcuts import render
from django.http import HttpResponse
from shared_resources import *

def index(request, special_request):
	initialize(request)
	
	output = ""
	output += """
	<script>
		window.location = "/users/";
	</script>
	"""
	
	return format_response("", output)
	
def teacher_default_availability(request, staff_id):
	initialize(request)
	output = ""
	
	title = t("Default schedule")
	
	has_rights = request.session["rights"]["view_staff"] or int(staff_id) == request.session["user"]["id"]
	if not has_rights:
		output += '<p class="error_message">'+t('You do not have the rights to view this staff member.')+'</p>'
	else:
		
		sql_connection = sqlite3.connect(global_vars["database"])
		sql_cursor = sql_connection.cursor()    
		
		result = sql_cursor.execute("""select first_name, last_name from staff where id = ?""", (staff_id,)).fetchone()
		if result == None:
			output += '<div class="col-xs-12"><p class="bg-warning">'+t('Staff member does not exist')+'</p></div>'
		else:
			output += schedule_edit(staff_id)

			teacher_info = {"id": staff_id, "name": result[0]}
			
			states = {}
			i = 0
			schedule = {0:{},1:{},2:{}}
			# Default schedule
			db_rows = sql_cursor.execute("""SELECT start_time, end_time, status FROM teacher_default_schedule where teacher_id = ?""", (staff_id,))
			for row in db_rows:
				schedule[0][i] = {"id": str(i), "start_time": row[0], "end_time": row[1], "type": row[2], "editable": 1, "layer": -1}
				i += 1

			title = t('Default schedule for %s', display_name(result[0],"",result[1]))
			
			output += schedule_html((1 if has_rights else 0), schedule)

	return format_response(version["icons"]["staff"]+title, output)
	
def javascript_language(request):
	
	return HttpResponse("test")